package com.chx.three.mama.fragments;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.widget.ViewFlipper;

import com.alibaba.fastjson.JSON;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.chx.three.mama.activities.ActionDetailActivity;
import com.chx.three.mama.R;
import com.chx.three.mama.activities.BeautyItemDetailActivity;
import com.chx.three.mama.activities.SocialMailDetailActivity;
import com.chx.three.mama.adapter.HomeBeautyAdapter;
import com.chx.three.mama.model.FlipperPic;
import com.chx.three.mama.model.HomeBeauty;
import com.chx.three.mama.view.ScrollGridView;
import com.chx.three.mama.view.ScrollListView;
import com.chx.three.mama.view.Topbar;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * 社交的fragment
 */
public class SocialFragment extends Fragment implements  View.OnClickListener, AdapterView.OnItemClickListener {

    private ViewFlipper viewFlipper;
    private RequestQueue requestQueue;
    private LinkedList<HomeBeauty> homeBeauties;
    private List<FlipperPic> flippers;

    public SocialFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View ret = inflater.inflate(R.layout.fragment_social, container, false);

        ImageView mail_btn = (ImageView)ret.findViewById(R.id.social_mail_btn);
        mail_btn.setOnClickListener(this);

        ScrollGridView gridView = (ScrollGridView) ret.findViewById(R.id.social_gridView);

        LinkedList<Map<String, ?>> data = new LinkedList<>();
        HashMap<String, Object> map = new HashMap<>();
        map.put("img",R.drawable.grid_img1);
        map.put("txt","同龄圈");
        data.add(map);

        map = new HashMap<>();
        map.put("img",R.drawable.grid_img2);
        map.put("txt","北京圈");
        data.add(map);

        map = new HashMap<>();
        map.put("img",R.drawable.grid_img3);
        map.put("txt","热帖");
        data.add(map);

        map = new HashMap<>();
        map.put("img",R.drawable.grid_img4);
        map.put("txt","发现美丽");
        data.add(map);

        map = new HashMap<>();
        map.put("img",R.drawable.grid_img5);
        map.put("txt","买买买");
        data.add(map);

        map = new HashMap<>();
        map.put("img",R.drawable.grid_img6);
        map.put("txt","不能吃");
        data.add(map);

        map = new HashMap<>();
        map.put("img",R.drawable.grid_img7);
        map.put("txt","心情日记");
        data.add(map);

        map = new HashMap<>();
        map.put("img",R.drawable.grid_img8);
        map.put("txt","好友圈");
        data.add(map);

        SimpleAdapter adapter = new SimpleAdapter(getActivity(),
                data,
                R.layout.item_social_group,
                new String[]{"img", "txt"},
                new int[]{R.id.item_social_grid_img, R.id.item_social_grid_txt}
        );
        gridView.setAdapter(adapter);

        requestQueue= Volley.newRequestQueue(getActivity());
        viewFlipper = (ViewFlipper) ret.findViewById(R.id.social_flipper);
        flippers=new LinkedList<>();

        String path1="http://www.qubaobei.com/ios/api/adr_ad.php?site=61&uid=4271726&user_type=2&baby_time=2015-03-22";
        final LinkedList<String> paths = new LinkedList<>();
        JsonObjectRequest filpperRequest=new JsonObjectRequest(path1,new JSONObject(),
                new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject object) {
                        try {
                            JSONArray jsonArray = object.getJSONArray("data");
                            Log.d("requestTest",object.toString());
                            flippers = JSON.parseArray(jsonArray.toString(), FlipperPic.class);

                            for (int i=0;i<flippers.size();i++){
                                final ImageView imageView=new ImageView(getActivity());
                                FlipperPic flipper = flippers.get(i);
                                String pic = flipper.getPic();
                                ImageRequest imageRequest=new ImageRequest(pic,
                                        new Response.Listener<Bitmap>() {
                                            @Override
                                            public void onResponse(Bitmap bitmap) {
                                                imageView.setImageBitmap(bitmap);
                                                Log.d("requestTest","pic success");
                                            }
                                        },0,0, Bitmap.Config.RGB_565,
                                        new Response.ErrorListener() {
                                            @Override
                                            public void onErrorResponse(VolleyError volleyError) {
                                                imageView.setImageResource(R.drawable.act_default);
                                                Log.d("requestTest","pic fail");
                                            }
                                        });
                                requestQueue.add(imageRequest);

                                //          ImageView imageView = getImageView(resId[i]);
                                viewFlipper.addView(imageView);
                            }

                            viewFlipper.setInAnimation(getActivity(),R.anim.left_in);
                            viewFlipper.setOutAnimation(getActivity(),R.anim.left_out);
                            viewFlipper.setFlipInterval(5000);
                            viewFlipper.startFlipping();

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(getActivity(), "未能获取到数据>.<~", Toast.LENGTH_SHORT).show();
            }
        });
        requestQueue.add(filpperRequest);
    //    int[] resId={R.drawable.act_default,R.drawable.act_over,R.drawable.act_start,R.drawable.act_tuan};

        homeBeauties=new LinkedList<HomeBeauty>();

        ScrollListView listView = (ScrollListView) ret.findViewById(R.id.social_listView);
        final HomeBeautyAdapter adapter1 = new HomeBeautyAdapter(homeBeauties, getActivity());
        listView.setAdapter(adapter1);
        listView.setOnItemClickListener(this);

        String path="http://www.qubaobei.com/ios/api/home_beauty.php?id=4271726";
        JsonObjectRequest request=new JsonObjectRequest(path,new JSONObject(),
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject object) {
                        JSONArray jsonArray = null;
                        try {
                            jsonArray = object.getJSONArray("data");
                            List<HomeBeauty> beauties = JSON.parseArray(jsonArray.toString(), HomeBeauty.class);
                            homeBeauties.addAll(beauties);
                            adapter1.notifyDataSetChanged();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(getActivity(), "未能获取到数据>.<~", Toast.LENGTH_SHORT).show();
            }
        });
        requestQueue.add(request);

        return ret;
    }

    private ImageView getImageView(int resId) {
        ImageView image = new ImageView(getActivity());
        image.setImageResource(resId);
        return image;
    }

    @Override
    public void onClick(View v) {
        Intent intent=new Intent(getActivity(), SocialMailDetailActivity.class);
        startActivity(intent);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent=new Intent(getActivity(), BeautyItemDetailActivity.class);
        HomeBeauty beauty = homeBeauties.get(position);
        String beautyId = beauty.getId();
        intent.putExtra("beautyId",beautyId);
        startActivity(intent);
    }
}
